<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    function testWeightBagProblem (weight, value, size) {
        // 定义 dp 数组
        const len = weight.length,
          dp = Array(len).fill().map(() => Array(size + 1).fill(0));

        // 初始化
        for (let j = weight[0]; j <= size; j++) {
          dp[0][j] = value[0];
        }

        // weight 数组的长度len 就是物品个数
        for (let i = 1; i < len; i++) { // 遍历物品
          for (let j = 0; j <= size; j++) { // 遍历背包容量
            if (j < weight[i]) dp[i][j] = dp[i - 1][j];
            else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
          }
        }

        console.table(dp)

        return dp[len - 1][size];
      }

      function test () {
        console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6));
      }

      test();
  </script>
</body>
</html>